package com.hejjon.graph.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hejjon.graph.bean.options.Options;
import com.hejjon.graph.dao.BaseNeo4jQueryDAO;
import com.hejjon.graph.service.Neo4jQueryService;
import com.hejjon.graph.service.OptionsService;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.types.Node;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: cs
 * @date: 2025-04-28 07:10:06
 * @since: 1.0
 */
@Service
public class Neo4jQueryServiceImpl implements Neo4jQueryService {

    @Resource
    private BaseNeo4jQueryDAO dao;

    @Resource
    private OptionsService optionsService;

    /**
     * 根据查询记录 形成标准的json格式数据
     * @param cypher
     * @return
     */
    @Override
    public JSONObject query(String cypher) {
        JSONObject resultObj = new JSONObject();
        JSONArray nodes = new JSONArray();
        Result result = dao.query(cypher);
        while (result.hasNext()) {
            Record record = result.next();
            Node node = record.get("n").asNode();
            Map<String, Object> tmp = node.asMap();
            Map<String, Object> map = new HashMap<>();
            map.put("name", tmp.get("name"));
            map.put("label", tmp.get("name"));
            nodes.add(map);
        }

        resultObj.put("nodes", nodes);
        Options options = optionsService.buildOptions(null);
        resultObj.put("options", options);

        System.out.println("===================图形JSON数据====================");
        System.out.println(resultObj.toJSONString());
        System.out.println("===================图形JSON数据====================");

        return resultObj;
    }

}
